<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // 业务id 有订单 生成 相同的业务id 为一组
        // 如果是改签 不需要生成这个记录
        // 子订单信息直接覆盖这些记录
        Schema::create('orders', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('channel_id')->comment('渠道id');
            $table->uuid('user_id')->comment('用户id');
            $table->string('order_sn', 32)->comment('子单号');
            $table->unsignedTinyInteger('transfer_flag')->default(0)->comment('中转标识 0 非中转 - 1 第一程 - 2 第二程');
            $table->unsignedTinyInteger('trip_no')->default(1)->comment('行程序号 - 1 第一程 - 2 第二程');
            $table->string('pay_sn', 32)->default('')->comment('支付单号');
            $table->unsignedDecimal('order_amount', 10, 2)->comment('订单金额');
            $table->string('trip_id', 32)->comment('行程业务id');
            $table->integer('order_state')->default(0)->comment('订单状态 0 待支付 10 已支付 30 已取消');
            $table->unsignedTinyInteger('trip_type')->default(1)->comment('订单类型 1 火车票 2 飞机票');
            $table->unsignedTinyInteger('delete_state')->default(0)->comment('删除状态 1 已删除 0 正常');
            $table->timestamp('deleted_at')->nullable()->comment('删除时间');
            $table->timestamp('trip_at')->nullable()->comment('出行时间');
            $table->timestamps();
            $table->unique('order_sn');
            $table->unique(['trip_id', 'trip_no'], 'unique_trip_id_no');
            $table->index(['user_id', 'delete_state', 'order_state'], 'idx_user_order_state');
            $table->index(['user_id', 'delete_state', 'created_at'], 'idx_created_at');
            $table->index(['user_id', 'delete_state', 'trip_at'], 'idx_trip_at');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('orders');
    }
};
